package com.caigou.modules.rank.ui

import android.os.Bundle
import android.view.MotionEvent
import android.view.inputmethod.InputMethodManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.container.BaseFragment
import com.caigou.modules.rank.databinding.ActivityRankBinding
import com.caigou.modules.rank.ui.charm.CharmRankFragment
import com.caigou.modules.rank.ui.contribution.ContributionRankFragment
import com.caigou.modules.rank.ui.fans.FansRankFragment
import com.caigou.modules.rank.ui.level.LevelRankFragment
import com.caigou.modules.rank.ui.master.MasterRankFragment
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/11/03
 */
@Route(path = RouterPath.Rank.PAGE_RANK)
@AndroidEntryPoint
class RankActivity : BaseActivity() {

    private lateinit var binding: ActivityRankBinding
    private var fansRankFragment: BaseFragment? = null
    private var levelRankFragment: BaseFragment? = null
    private var charmRankFragment: BaseFragment? = null
    private var contributionRankFragment: BaseFragment? = null
    private var masterRankFragment: BaseFragment? = null


    @Autowired(name = AppConstants.TARGET_FRAGMENT_TAG)
    lateinit var targetFragmentTag: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRankBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.rankFlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)
        switchFragment(targetFragmentTag)
    }

    override fun switchFragment(tag: String) {
        when (tag) {
            RouterPath.Rank.TAG_FANS_RANK -> {
                if (fansRankFragment == null) {
                    fansRankFragment = FansRankFragment()
                }
                switchFragment(fansRankFragment!!, tag)
            }
            RouterPath.Rank.TAG_LEVEL_RANK -> {
                if (levelRankFragment == null) {
                    levelRankFragment = LevelRankFragment()
                }
                switchFragment(levelRankFragment!!, tag)
            }
            RouterPath.Rank.TAG_CHARM_RANK -> {
                if (charmRankFragment == null) {
                    charmRankFragment = CharmRankFragment()
                }
                switchFragment(charmRankFragment!!, tag)
            }
            RouterPath.Rank.TAG_CONTRIBUTION_RANK -> {
                if (contributionRankFragment == null) {
                    contributionRankFragment = ContributionRankFragment()
                }
                switchFragment(contributionRankFragment!!, tag)
            }
            RouterPath.Rank.TAG_MASTER_RANK -> {
                if (masterRankFragment == null) {
                    masterRankFragment = MasterRankFragment()
                }
                switchFragment(masterRankFragment!!, tag)
            }
        }
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        ev?.let {
            if (it.action == MotionEvent.ACTION_DOWN) {
                val v = currentFocus
                if (isShouldHideInput(v, it)) {
                    val imm =
                        this@RankActivity.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                    imm.hideSoftInputFromWindow(v!!.windowToken, 0)
                }
                return super.dispatchTouchEvent(it)
            }
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        return if (window.superDispatchTouchEvent(ev)) {
            true
        } else onTouchEvent(ev)
    }

    override fun onBackPressed() {
        if (fragmentStack.isNotEmpty() && fragmentStack.size > 1) {
            fragmentStack.removeLast()
            switchFragment(fragmentStack.last)
        } else {
            super.onBackPressed()
        }
    }
}